const cp = require('cp');
require('./page.css');
let moduleExport = {};
const obj_m = {
  'myshop': {
    name: '我的店铺',
    open: true,
  },
  'shoporg': {
    name: '店铺组织',
    open: true,
  },
  'service': {
    name: '我是服务商',
    open: true,
  },
  'third': {
    name: '第三方服务',
    open: true,
  },
};
const obj_p = {
  'myshop': {'apply': '开通签约', 'decoration': '装修店铺', 'shophome': '店铺主页'},
  'shoporg': {'linkorg': '关联组织', 'manage': '店员管理'},
  'service': {'legalize': '认证签约', 'mine': '我的服务（租赁/服务）', 'demand': '我的需求'},
  'third': {'wxpublic': '微信公众号', 'wxapp': '微信小程序', 'alipay': '支付宝生活号', 'alipayapp': '支付宝小程序'},
};
const obj_son = {
  'step1': {
    'edit': '申请开通',
    'view': '查看资料',
  },
  'step2': {
    'edit': '申请开通',
    'view': '查看合同',
  },
  'step3': {
    'edit': '店铺模板',
    'view': '全部模板',
  },
  'step4': {
    'edit': '第三方授权',
    'view': '第三方授权',
  },
};
let vm_menu = avalon.define({
  $id: 'mc_sidemenu',
  obj_m: obj_m,
  obj_p: obj_p,
  process: -1,
  currentUrl: 'myshop/apply',
  fn: (el, index, key) => {
    console.log(key);
    return index === key;
  },
  booleanShow: (status, modulename, pagename) => {
    // if (status < 3 && modulename === 'myshop' && pagename !== 'apply') {
    //   return false;
    // } else {
    //   return true;
    // }
    return true;
  },
  init: () => {
    cp.ajaxUrl({
      controller: 'shopskt',
      cmd: 'com.rel.porg.pb.shop_sel_skt_req',
      async: false,
      data: {},
      success: res => {
        vm_menu.process = res.sktinfo.process;
        if (res.sktinfo.process < 3) {
          avalon.router.navigate('#!/myshop/apply', 2);
        } else {
          avalon.router.navigate('#!/myshop/decoration', 2);
        }
      },
      fail: res => {
        vm_menu.process = -1;
        avalon.router.navigate('#!/myshop/apply', 2);
      },
    });
  },
});
require('router/mmRouter');
let vm_router = avalon.define({
  $id: 'mc_router',
  test: '111',
  s_modulename: '我的店铺',
  s_pagename: '开通签约',
  s_sonname: '',
  s_fourth: '',
  page: '这里是页面',
});
// 一级页面 (模块名、页面名)
avalon.router.add('/:modulename/:pagename', (modulename, pagename) => {
  vm_menu.currentUrl = modulename + '/' + pagename;
  vm_router.s_modulename = obj_m[modulename].name;
  vm_router.s_pagename = obj_p[modulename][pagename];
  vm_router.s_sonname = '';
  vm_router.s_fourth = '';
  let page = require('../../_router/' + modulename + '/' + pagename + '/page.js');
  vm_router.page = page.content;
  if (pagename === 'shophome') {
    avalon.ready(() => {
      page.vm.initEcharts();
    });
  }
});
// 开通签约子级页面（步骤名、页面名）
avalon.router.add('/myshop/apply/:step/:sonname', (step, sonname) => {
  vm_menu.currentUrl = 'myshop/apply';
  vm_router.s_modulename = obj_m['myshop'].name;
  vm_router.s_pagename = obj_p['myshop']['apply'];
  vm_router.s_sonname = obj_son[step][sonname];
  vm_router.s_fourth = '';
  let page = require('../../_router/myshop/' + step + '/' + sonname + '/page.js');
  vm_router.page = page.content;
  if (step === 'step3') {
    avalon.ready(() => {
      page.vm.initRaty();
    });
  }
});
// 开通签约子级页面店铺模板详情
avalon.router.add('/myshop/apply/step3/detail/:id', (id) => {
  let tempid = Number(id.substring(12, id.length));
  vm_menu.currentUrl = 'myshop/apply';
  vm_router.s_modulename = obj_m['myshop'].name;
  vm_router.s_pagename = obj_p['myshop']['apply'];
  let page = require('../../_router/myshop/step3/detail/page.js');
  vm_router.page = page.content;
  page.vm.init(tempid);
  vm_router.s_sonname = '店铺模板';
  vm_router.s_fourth = page.vm.tempinfo.title;
});
// 启动路由监听
avalon.history.start({
  root: '/act/index/index/page.html',
  hashPrefix: '',
});
vm_menu.init();
moduleExport.vm_menu = vm_menu;
moduleExport.vm_router = vm_router;
module.exports = moduleExport;
